package com.mryin.performance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mryin.performance.mapper.TaskMapper;
import com.mryin.performance.pojo.Task;
import com.mryin.performance.service.TaskService;
import com.mryin.performance.utils.ExcelUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MRYIN
 * @since 2021-03-02
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Override
    public List<Task> getAllFinishTask(String name) {
        return taskMapper.getAllFinishTask(name);
    }

    @Override
    public List<Task> getAllNormalFinishTask(String name) {
        return taskMapper.getAllNormalFinishTask(name);
    }

    @Override
    public List<Task> getAllUnFinishTask(String name) {
        return taskMapper.getAllUnFinishTask(name);
    }

    @Override
    public List<Task> getAllWaitFinishTask(String name) {
        return taskMapper.getAllWaitFinishTask(name);
    }

    @Override
    public List<Task> getAllOverTimeFinishTask(String name) {
        return taskMapper.getAllOverTimeFinishTask(name);
    }

    @Override
    public Double getAllFinishTaskMonovalent(String name) {
        return taskMapper.getAllFinishTaskMonovalent(name);
    }

    @Override
    public int getRankingByNameOnMonovalent(String name) {
        return taskMapper.getRankingByNameOnMonovalent(name);
    }

    @Override
    public int getRankingByNameOnMarketPrice(String name) {
        return taskMapper.getRankingByNameOnMarketPrice(name);
    }

    @Override
    public int getActualWorkDay() {
        return taskMapper.getActualWorkDay();
    }

    @Override
    public Task getTaskById(String id) {
        return taskMapper.getTaskById(id);
    }

    @Override
    public String inputExcel(MultipartFile file, HttpServletRequest request , HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        if(file.isEmpty()){
            try {
                throw new Exception("文件不存在！");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        InputStream in =null;
        try {
            in = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<List<Object>> listob = null;
        try {
            listob = new ExcelUtils().getBankListByExcel(in,file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
        for (int i = 0; i < listob.size(); i++) {
            System.out.println("我是listob里面的值：" + i);
            List<Object> lo = listob.get(i);

            for (int j = 0 ; j < lo.size() ; j ++) {
                System.out.println("我是lo里面的值"+ j);
                Task taskById = taskMapper.getTaskById((String) lo.get(0));
                if(taskById != null){
                    return "已经存在词条任务";
                }else{
                    Task vo = new Task();
                    System.out.println((String.valueOf(lo.get(0))));
                    System.out.println(String.valueOf(lo.get(1)));
                    System.out.println(String.valueOf(lo.get(2)));
                    System.out.println(String.valueOf(lo.get(3)));
                    //vo.setId()
                    vo.setId(String.valueOf(String.valueOf(lo.get(0))));
                    vo.setMonovalent(Integer.valueOf(String.valueOf(lo.get(1))));
                    vo.setMarketprice(Integer.valueOf(String.valueOf(lo.get(2))));
                    vo.setId(String.valueOf(String.valueOf(lo.get(3))));
                    taskMapper.insert(vo);
                }

            }
            /*
            System.out.println("遍历" + listob.get(i));





            try {
                j = taskMapper.getTaskById((String) lo.get(0));

            } catch (NumberFormatException e) {
                System.out.println("没有新增");
            }
            int size = lo.size();
            System.out.println("我是lo的size"+size);
            Object o = lo.get(0);
            System.out.println("我是lo的第一个"+o);
            //vo.setName(String.valueOf(String.valueOf(lo.get(3))));

            //      System.out.println("这个开始");
            //      System.out.println(vo.toString());
            //      System.out.println("这个结束");
            if(j == null)
            {
                // System.out.println("判断j为空的话");
                taskMapper.insert(vo);
            }
            else
            {
                //    System.out.println("判断j为不空的话");
                taskMapper.updateById(vo);
            }*/
        }
        return "导入成功";
    }

    @Override
    public int addTaskByReadExcel(Task task) {
        return taskMapper.addTaskByReadExcel(task);
    }


}
